package routers

import (
	_ "gin_blog/docs"
	"gin_blog/middleware/cors"
	"gin_blog/middleware/jwt"
	"gin_blog/pkg/setting"
	"gin_blog/pkg/upload"
	"gin_blog/routers/api"
	v1 "gin_blog/routers/api/v1"
	"github.com/gin-gonic/gin"
	swaggerFiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
	"net/http"
)

func InitRouter() *gin.Engine {
	r :=gin.New()
	r.Use(gin.Logger())

	r.Use(gin.Recovery())
	r.Use(cors.Cors())

	gin.SetMode(setting.ServerSetting.RunMode)

	r.GET("/test", func(c *gin.Context) {
		c.JSON(http.StatusOK,gin.H{
			"message":"test",
		})
	})
	// 开启swag
	r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))

	//获取token
	r.POST("/login",api.Login)
	//注册
	r.POST("/register",api.Register)
	//用户信息
	r.GET("/userinfo",jwt.JWT(),api.UserInfo)
	r.POST("/uploadImage",api.UploadImage)
	r.StaticFS("/upload/images", http.Dir(upload.GetIamgeFullPath()))
	apiv1 :=r.Group("/api/v1")
	apiv1.Use(jwt.JWT())
	{
		//标签路由
		apiv1.GET("/tags",v1.GetTags)
		apiv1.POST("/tags",v1.AddTag)
		apiv1.PUT("/tags/:id",v1.EditTag)
		apiv1.DELETE("/tags/:id",v1.DeleteTag)

		//文章路由
		//获取文章列表
		apiv1.GET("/articles",v1.GetArticles)
		//获取指定文章
		apiv1.GET("/article/:id",v1.GetArticle)
		apiv1.POST("/article",v1.AddArticle)
		apiv1.PUT("/article/:id",v1.EditArticle)
		apiv1.DELETE("/article/:id",v1.DeleteArticle)

	}

	return r
}